﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Cashier_Model;
using Cashier_Service;
using Cashier.usercontrols;
using Cashier.utils;

namespace Cashier.member
{
    public partial class MemberClearIntegralForm : Form
    {
        private static MemberClearIntegralForm memberClearIntegralForm;
        private MainForm mainform;
        private DataGridViewExt dgv;
        private bool isSave = false;
        private MemberService memberService;

        /// <summary>
        /// 私有构造函数
        /// </summary>
        /// <param name="mainform"></param>
        /// <param name="dgv"></param>
        private MemberClearIntegralForm(MainForm mainform, DataGridViewExt dgv)
        {
            InitializeComponent();
            this.mainform = mainform;
            this.dgv = dgv;
            this.memberService = new MemberService();
            this.ControlsSetRights();
        }

        /// <summary>
        /// 获取MemberClearIntegralForm实例
        /// </summary>
        /// <param name="mainform"></param>
        /// <param name="dgv"></param>
        /// <returns></returns>
        public static MemberClearIntegralForm GetMemberClearIntegralForm(MainForm mainform, DataGridViewExt dgv)
        {
            if (memberClearIntegralForm == null)
                memberClearIntegralForm = new MemberClearIntegralForm(mainform, dgv);
            memberClearIntegralForm.textBox_reason.Text = String.Empty;
            return memberClearIntegralForm;
        }

        /// <summary>
        /// 控件设置权限
        /// </summary>
        private void ControlsSetRights()
        {
            Button[] buttons = { button_save };
            RightsforControls.RightsforButton(mainform.Admin.RolePurviewtable, buttons);
        }

        #region 重写方法

        /// <summary>
        /// 重写键盘事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool b = base.ProcessCmdKey(ref msg, keyData);
            switch (keyData)
            {
                case Keys.Escape:
                    isSave = false;
                    this.Close();
                    break;
                case Keys.Enter:
                    if (!button_save.Focus())
                    {
                        isSave = true;
                        this.Close();
                    }
                    break;
            }
            return b;
        }

        #endregion

        #region 控件事件

        /// <summary>
        /// 取消按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_esc_Click(object sender, EventArgs e)
        {
            this.isSave = false;
            this.Close();
        }

        /// <summary>
        /// 保存按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_save_Click(object sender, EventArgs e)
        {
            this.isSave = true;
            this.Close();
        }

        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MemberClearIntegralForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.isSave)
            {
                String reason = textBox_reason.Text.Trim();
                if (String.IsNullOrEmpty(reason))
                {
                    e.Cancel = true;
                    MessageBox.Show("请输入清零理由！");
                    return;
                }
                Admin admin = this.mainform.Admin;
                MemberIntegral memberIntegral = new MemberIntegral();
                memberIntegral.EnterpriseNo = admin.EnterpriseNo;
                memberIntegral.StoreNo = admin.StoreNo;
                memberIntegral.AdminUsername = admin.Username;
                memberIntegral.AdminName = admin.Name;
                memberIntegral.TransNo = String.Empty;
                memberIntegral.TransTime = DateTime.Now;
                memberIntegral.Description = textBox_reason.Text.Trim();

                int count = 0;
                foreach (DataGridViewRow row in this.dgv.SelectedRows)
                {
                    String memberNo = row.Cells["member_no"].Value.ToString();
                    memberIntegral.MemberNo = memberNo;
                    memberIntegral.Integral = Int32.Parse(row.Cells["current_integral"].Value.ToString());
                    int flag = this.memberService.ClearMemberIntegral(memberNo, memberIntegral);
                    if (flag > 0)
                        count++;
                }

                if (count == this.dgv.SelectedRows.Count)
                {
                    MessageBox.Show("清零成功！");
                    this.dgv.LoadingData();
                }
                else
                {
                    MessageBox.Show("清零失败！");
                }
            }
        }

        #endregion

    }
}
